﻿-----------Dirty Read------------

--Đăng ký chuyến xe
alter proc usp_DangKyChuyenXe
@maNV char(10),
@ngayDi datetime,
@maChuyen char(10),
@soHieu char(10)
as
begin tran
	begin try
		INSERT INTO DANGKY VALUES(@maNV,@ngayDi,@maChuyen,@soHieu)
		waitfor delay '00:00:10'
		declare @loai char(5)
		set @loai = (SELECT LOAINV FROM NhanVien WHERE MANV = @maNV)
		if(convert(varchar(1),@loai,103) = '1')
		begin
			declare @sotai int 
			set @sotai = (SELECT COUNT(*) FROM DANGKY dk,NHANVIEN nv WHERE dk.MaNV = nv.MaNV AND nv.LoaiNV = '1' AND NgayDi = @ngayDi AND MaChuyen = @maChuyen AND SoHieu = @soHieu)
			declare @sotaitoida int
			set @sotaitoida =  (SELECT SOTAI FROM LICHDI WHERE NgayDi = @ngayDi AND MaChuyen = @maChuyen AND SoHieu = @soHieu)
			if( @sotai > @sotaitoida)
			begin
				declare @error varchar(1000)
				set @error = N'Đã đủ số tài'
				raiserror (@error,16,1)
				rollback tran
				return
			end
		end

		if(convert(varchar(1),@loai,103) = '0')
		begin
			declare @solo int 
			set @solo = (SELECT COUNT(*) FROM DANGKY dk,NHANVIEN nv WHERE dk.MaNV = nv.MaNV AND nv.LoaiNV = '0' AND NgayDi = @ngayDi AND MaChuyen = @maChuyen AND SoHieu = @soHieu)
			declare @solotoida int
			set @solotoida =  (SELECT SOLO FROM LICHDI WHERE NgayDi = @ngayDi AND MaChuyen = @maChuyen AND SoHieu = @soHieu)
			if( @solo > @solotoida)
			begin
				declare @error1 varchar(1000)
				set @error1 = N'Đã đủ số lơ'
				raiserror (@error1,16,1)
				rollback tran
				return
			end
		end
		print N'Đăng ký thành công'
		commit tran
	end try
	begin catch
	    declare @error2 varchar(1000)
		set @error2 = N'Lỗi : ' + error_message()
		raiserror (@error2,16,1)
		rollback tran
		return
	end catch
go

--Quản lý khác xem thông tin lịch đi

alter proc usp_XemDanhSachDangKy
@ngayDi datetime
as
begin tran
	set tran isolation level read uncommitted
	begin try
		SELECT * FROM DANGKY WHERE NgayDi = @NgayDi
	end try
	begin catch
		declare @error varchar(1000)
		set @error = N'Lỗi: ' + error_message()
		raiserror (@error,16,1)
		rollback tran
		return
	end catch
commit tran



